package com.haogu.controller;

import com.haogu.entity.PTask;
import com.haogu.entity.Task;
import com.haogu.entity.User;
import com.haogu.service.TaskService;
import com.haogu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/task")
public class TaskController {
    @Resource
    private UserService userService;

    @Resource
    private TaskService taskService;
    /**
     * 去task页面
     * @return
     */
    @RequestMapping("toTask")
    public String toTask(){
        return "task/task";
    }

    /**
     * 去list_task页面
     * @return
     */
    @RequestMapping("/toListTask")
    public String toListTask(){
        return "task/list_task";
    }

    @RequestMapping("/listTask")
    @ResponseBody
    public Map listTask(PTask p,HttpSession session){
        p.setUser((User)session.getAttribute("user"));
        return taskService.listTask(p);
    }

    /**
     * 添加
     */
    @RequestMapping("/toAddTask")
    public String toAddTask(Model model){
        List<User> users = userService.selectAllUser();
        model.addAttribute("users",users);
        return "task/add_task";
    }

    @RequestMapping("/addTask")
    @ResponseBody
    public Map addTask(Task task,HttpSession session){
        return taskService.addTask(task,(User) session.getAttribute(("user")));
    }

    /**
     * 查看
     */
    @RequestMapping("/seeTask")
    public String seeTask(Integer id,Model model){
        taskService.seeTask(id,model);
        return "task/see_task";
    }
    /**
     * 删除
     */
    @RequestMapping("/deleteTask")
    @ResponseBody
    public Map deleteTask(String ids,HttpSession session){
        return taskService.deleteTask(ids,(User)session.getAttribute("user"));
    }

    /**
     * 修改
     */
    @RequestMapping("/toUpdateTask")
    public String toUpdateTask(Integer id,Model model){
        taskService.toUpdateTask(id,model);
        return "task/modify_task";
    }
    @RequestMapping("/updateTask")
    @ResponseBody
    public Map updateTask(Task task){
        return taskService.updateTask(task);
    }

    /**
     * 关闭
     */
    @RequestMapping("/closeTask")
    @ResponseBody
    public Map closeTask(Integer id){
        return taskService.closeTask(id);
    }
    /**
     * ---------------------------以下是回收站中的内容-----------------------------------
     */
    @RequestMapping("/toListTaskHuishou")
    public String toListTaskHuishou(){
        return "task/list_task_huishou";
    }

    @RequestMapping("/listTaskHuiShou")
    @ResponseBody
    public Map listTaskHuiShou(Integer page,Integer rows,String subject,Integer select){

        return taskService.listTaskHuishou(page,rows,subject,select);
    }

    @RequestMapping("/toDeleteTaskHuishou")
    @ResponseBody
    public Map toDeleteTaskHuishou(Integer id){
        return taskService.toDeleteTaskHuishou(id);
    }

    /**
     * 清空所有
     */
    @RequestMapping("/toClearAll")
    @ResponseBody
    public Map toClearAll(){
        return taskService.toClearAll();
    }

    /**
     * 恢复
     */
    @RequestMapping("/huiFuTask")
    @ResponseBody
    public Map huiFuTask(Integer id){
        return taskService.huiFuTask(id);
    }
}
